package com.hsgene.common.util.validation;

import java.lang.reflect.Field;

/**
 * 对象检查工具类
 *
 * @author wangbing
 * @version 1.0, 2018/7/5
 */
public class ObjectChecker {

    /**
     * 检测对象是否为空
     *
     * @param obj        被检测对象
     * @param checkField 是否要检测对象属性
     * @return true 对象为空
     */
    public static boolean isNull(Object obj, boolean checkField) {
        boolean isNull = obj == null;
        if (!isNull && checkField) {
            try {
                for (Field field : obj.getClass().getDeclaredFields()) {
                    field.setAccessible(true);
                    isNull = field.get(obj) == null;
                    if (!isNull) {
                        break;
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            isNull = true;
        }
        return isNull;
    }
}
